package com.stardata.liangshisq.tcslmon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stardata.liangshisq.tcslmon.entity.DbiEmp;
import com.stardata.liangshisq.tcslmon.entity.OrderVo;
import com.stardata.liangshisq.tcslmon.entity.SaleDetail;
import com.stardata.liangshisq.tcslmon.mapper.DbiEmpMapper;
import com.stardata.liangshisq.tcslmon.service.DbiEmpService;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Samson Shu
 * @email shush@stardata.top
 * @date 2020/7/31 14:56
 */
@Service
@AllArgsConstructor
public class DbiEmpServiceImpl extends ServiceImpl<DbiEmpMapper, DbiEmp>
    implements DbiEmpService {

  private final DbiEmpMapper dbiEmpMapper;

  @Override
  public DbiEmp verifyEmpLogin(Long shopId, String empNo, String password) {
    QueryWrapper<DbiEmp> wrapper = new QueryWrapper<>();
    wrapper.eq("code", empNo)
        .eq("belong_shop_id", shopId)
        .eq("delflg", 0);
    List<DbiEmp> list = dbiEmpMapper.selectList(wrapper);

    return (list.size()<1)? null: list.get(0);
  }

  @Override
  public void fillServerNameForOrders(@NotNull List<OrderVo> orderVos) {
    orderVos.forEach(orderVo -> {
      //根据创建人id找到其姓名
      DbiEmp dbiEmp = dbiEmpMapper.selectById(orderVo.getServerId());
      if (dbiEmp != null) {
        orderVo.setServerName(dbiEmp.getName());
      }
      else {
        orderVo.setServerName("");
      }
    });
  }

  @Override
  public void fillWaiterCodeForSaleDetails(List<SaleDetail> saleDetails) {
    saleDetails.forEach(saleDetail -> {
      //根据waiterId找到其工号
      DbiEmp dbiEmp = dbiEmpMapper.selectById(saleDetail.getWaiterId());
      if (dbiEmp != null) {
        saleDetail.setWaiterCode(dbiEmp.getCode());
      }
    });
  }

}
